package com.cynoware.busappmas.contentprovider;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import com.cynoware.busappmas.model.ConfigInfoModel;

public class DbConfig{
	private ContentResolver mContentResolver;
	
	public DbConfig(ContentResolver cr){
		mContentResolver = cr;
	}
	
	public Map<String, ConfigInfoModel> getConfigs(){
		Map<String, ConfigInfoModel> maps = new HashMap<String, ConfigInfoModel>();
		String[] projection ={DbHelper.COLUMN_CONFIGS_NAME, DbHelper.COLUMN_CONFIGS_VALUE,DbHelper.COLUMN_CONFIGS_STATE};
		Uri uri = DbContentProvider.CONTENT_URI_CONFIGS;
		Cursor cursor = mContentResolver.query(uri, projection, 
				null, null, null);
		cursor.moveToFirst();
		ConfigInfoModel info=null;
	    while (!cursor.isAfterLast()) {
	    	info=new ConfigInfoModel();
	    	info.setValue(cursor.getString(1));
	    	info.setState(cursor.getString(2));
			maps.put(cursor.getString(0),info);
		    cursor.moveToNext();
		}	
	    cursor.close();
		return maps;		
	}
	
	
	
	public void createConfig(String name, String value, String state){
		ContentValues cv = new ContentValues();
		cv.put(DbHelper.COLUMN_CONFIGS_NAME, name);
		cv.put(DbHelper.COLUMN_CONFIGS_VALUE, value);
		cv.put(DbHelper.COLUMN_CONFIGS_STATE, state);
		Uri uri = DbContentProvider.CONTENT_URI_CONFIGS;
		mContentResolver.insert(uri,  cv);
	}
	
	public void updateConfig(String name, String value, String state){
		ContentValues cv = new ContentValues();
		cv.put(DbHelper.COLUMN_CONFIGS_VALUE, value);
		cv.put(DbHelper.COLUMN_CONFIGS_STATE, state);
		Uri uri = DbContentProvider.CONTENT_URI_CONFIGS;
		String selection = "name=?";
		String[] selectionArgs={name};
		mContentResolver.update(uri, cv, selection, selectionArgs);
	}
}
